package cn.ysnk.clz.controller.generatCode;

import java.sql.SQLException;
import java.util.List;

import com.alibaba.fastjson.JSONObject;
import com.jfinal.core.Controller;

import cn.ysnk.clz.config.annotation.ControllerAnnotation;
import cn.ysnk.clz.entity.generatorCode.EntityColumnEntity;
import cn.ysnk.clz.interceptor.security.role.RoleEnum;
import cn.ysnk.clz.util.GeneratorUtil;
import cn.ysnk.generateCode.TabelGenerator;

@ControllerAnnotation(url="/GeneratorCode",roleType={RoleEnum.ROLE_ADMIN,})
public class GeneratorCodeController extends Controller {
	public void index() {
		renderJsp("/admin/generateCode/index.jsp");
	}
	public void getTables(){
		try {
			List<String> tables = GeneratorUtil.getAllTables();
			JSONObject result = new JSONObject();
			result.put("error", 0);
			result.put("data", tables);
			renderJson(result.toJSONString());
		} catch (SQLException e) {
			e.printStackTrace();
			renderJson("{\"error\":1,\"message\":\"未获取到表\"}");
		}
	}
	public void getTableInfo(){
		String tableName = getPara("tableName");
		try {
			List<EntityColumnEntity> colunms = GeneratorUtil.getTabelInfo(tableName);
			JSONObject result = new JSONObject();
			result.put("error", 0);
			result.put("data", colunms);
			renderJson(result.toJSONString());
		} catch (SQLException e) {
			e.printStackTrace();
			renderJson("{\"error\":1,\"message\":\"未获取到表信息\"}");
		}
	}
	public void generatorCode() {
		String name = getPara("name");
		String url = getPara("url");
		String tabelName = getPara("tabelName");
		String searchColumn = getPara("searchColumn");
		String listColumn = getPara("listColumn");
		String formColumn = getPara("formColumn");
		String projectPath = getPara("projectPath");
		TabelGenerator.generateTable(name, url, tabelName, searchColumn, formColumn, listColumn,projectPath);
	}
}
